package com.biyao.service.model.login.imp;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.util.*;

import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.biyao.schedulingdao.login.TUserMapper;
import com.biyao.schedulingentity.login.TUserEntity;
import com.biyao.service.model.login.LoginMessage;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * 
 *
 * @author code generator
 * @date 2019-09-16 14:10:02
 */
@Service
public class TUserService extends ServiceImpl<TUserMapper, TUserEntity> implements LoginMessage {

    public Page list(int pageNum, int pageSize){

        EntityWrapper<TUserEntity> tUserentityWrapper=new EntityWrapper<>();
        Page page=selectPage(new Page(pageNum,pageSize),tUserentityWrapper);
        page.setRecords(formatRecords(page.getRecords()));
        return page;
    }

    List formatRecords(List<TUserEntity> list){
        List resList=new ArrayList();
        for(TUserEntity tUserEntity:list){
            Map map=formatEntity(tUserEntity);
            resList.add(map);
        }
        return resList;
    }

    Map formatEntity(TUserEntity tUserEntity){
        Map map=new HashMap(16);
        //登录用户
        if(tUserEntity.getId()!=null){
           map.put("id",tUserEntity.getId());
        }
        //账号
        if(tUserEntity.getAccount()!=null){
           map.put("account",tUserEntity.getAccount());
        }
        //密码
        if(tUserEntity.getPassword()!=null){
           map.put("password",tUserEntity.getPassword());
        }
        return map;
    }

    @Override
    public TUserEntity selectIsExistUser(String account,String password) {
        EntityWrapper<TUserEntity> tUserentityWrapper=new EntityWrapper<>();
        tUserentityWrapper.eq("account",account);
        List<TUserEntity> list=selectList(tUserentityWrapper);
        if(CollectionUtils.isEmpty(list)){
            return  null;
        }
        TUserEntity tUserEntity=list.get(0);
        if (tUserEntity == null || !ObjectUtils.nullSafeEquals(tUserEntity.getPassword(), password)) {
            return null;
        }
        return tUserEntity;
    }
}

